package org.noootsab.gae.games.tzaar.model;

import com.google.appengine.api.datastore.*;
import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.*;
import java.io.*;
import java.util.*;

/**
 * User: sandy
 * Date: 21-mars-2010
 * Time: 19:32:30
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Player implements Externalizable {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    @Unique
    private String userId;

    @Persistent()
    @Unique
    private Email email;

    public Player() {
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Email getEmail() {
        return email;
    }

    public void setEmail(Email email) {
        this.email = email;
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        String s = (String) in.readObject();
        if (s != null) {
            key = KeyFactory.stringToKey(s);
        }

        userId = (String) in.readObject();

        email = new Email((String) in.readObject());
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        if (key != null) {
            out.writeObject(KeyFactory.keyToString(key));
        }

        out.writeObject(userId);

        out.writeObject(email.getEmail());
    }
}
